package cc.fs.myproject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cc.fs.myproject.utils.EvenBus;
import rx.Subscriber;
import rx.subjects.Subject;

public class RxAndroidActivity extends BaseActivity {

    @InjectView(R.id.id_toolbar)
    Toolbar idToolbar;
    @InjectView(R.id.publish)
    Button publish;
    @InjectView(R.id.remove)
    Button remove;
    @InjectView(R.id.subscribe1)
    Button subscribe1;
    @InjectView(R.id.subscribe2)
    Button subscribe2;

    String chanel = "mine";


    private int msg = 0;
    private Subject subject1;
    private Subject subject2;
    private Subscriber subscriber;
    private Subscriber subscriber2;

    public static void start(Context context) {
        Intent intent = new Intent(context, RxAndroidActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rx_android);
        ButterKnife.inject(this);

//        final Subject subject = PublishSubject.create();
//        subject.observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observable.OnSubscribe() {
//                    @Override
//                    public void call(Object o) {
//                        Log.e("------->", o.toString());
//                    }
//                });
//        subject.onNext("one");
//        subject.onNext("two");
//        subject.onNext("three");


        subscriber = new Subscriber() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object o) {
                Log.e("---订阅1---->", o.toString());
            }
        };

        subscriber2 = new Subscriber() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object o) {
                Log.e("---订阅2---->", o.toString());
            }
        };

        subscribe1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               subject1 = EvenBus.register(chanel, subscriber);
            }
        });

        subscribe2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               subject2 = EvenBus.register(chanel, subscriber2);
            }
        });


        publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EvenBus.post(chanel, "123456789");
            }
        });

        remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                subscriber.unsubscribe();
                EvenBus.unRegister(chanel, subject1);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_rx_android, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
